package com.appkubes.mall.admin.application.order.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.constants.BaseConstants;
import com.appkubes.common.exception.BusinessException;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.order.order.entity.vo.OrderShipVO;
import com.appkubes.mall.admin.application.order.order.service.OrderService;
import com.appkubes.mall.admin.application.plugin.rent.service.PluginRentService;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.core.service.impl.BaseMallOrderServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OrderServiceImpl extends BaseMallOrderServiceImpl implements OrderService {
    @Resource
    PluginRentService pluginRentService;

    @Override
    public Boolean ship(OrderShipVO orderShipVO) {
        MallOrder order = this.getById(orderShipVO.getOrderKey());
        order.setStatus(BaseConstants.ORDER.SENDING);
        BeanUtils.copyProperties(orderShipVO, order);
        order.setFlexSegment3(orderShipVO.getImei());
        order.setExpireTime(DateUtils.DateToTimestamp(new Date()) + 86400);
        Boolean flag = this.saveOrUpdate(order);
        if (flag) {
            //更新成功，通知信贷系统
            JSONObject jsonObject = pluginRentService.getOrderState(orderShipVO.getOrderKey());
            if (jsonObject.getInteger("isSure") != 1) {
                throw new BusinessException("租赁单未确认");
            }
        }
        return flag;
    }

    @Override
    public Boolean addImei(OrderShipVO orderShipVO) {
        MallOrder order = this.getById(orderShipVO.getOrderKey());
        order.setFlexSegment3(orderShipVO.getImei());
        return this.saveOrUpdate(order);
    }
}
